package com.ruoyi.lightkillPest.service.impl;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.lightkillPest.domain.device.SCDDeviceDao;
import com.ruoyi.lightkillPest.domain.device.SelectSCDDeviceDao;
import com.ruoyi.lightkillPest.mapper.SCDDeviceMapper;
import com.ruoyi.lightkillPest.service.SCDDeviceService;
import com.ruoyi.permission.Dao.SerDevicePermi;
import com.ruoyi.permission.mapper.SerDevicePermiMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.github.pagehelper.page.PageMethod.startPage;

@Service
@Slf4j
public class SCDDeviceServiceImpl extends BaseController implements SCDDeviceService {

    @Autowired
    SCDDeviceMapper scdDeviceMapper;
    @Autowired
    SerDevicePermiMapper devicePermiMapper;

    /**
     * 获取所有设备信息
     * @return
     */
    @Override
    public TableDataInfo selectAllDeviceInfo() {
        startPage();
        List<SCDDeviceDao> deviceInfos = scdDeviceMapper.selectAllDeviceInfo();
        return getDataTable(deviceInfos);
    }

    /**
     * 根据条件查询设备
     * @param deviceInfo
     * @return
     */
    @Override
    public TableDataInfo selectDeviceInfoByCondition(SCDDeviceDao deviceInfo,List<SerDevicePermi> serDevicePermiList){
        startPage();
        List<SCDDeviceDao> deviceInfos = scdDeviceMapper.selectDeviceInfoByCondition(deviceInfo);
        if(!serDevicePermiList.isEmpty()) {
            deviceInfos = deviceInfos.stream()
                    .filter(filteredDevcieInfo -> serDevicePermiList.stream()
                            .anyMatch(serDevicePermi -> serDevicePermi.getDeviceNo().equals(filteredDevcieInfo.getId())))
                    .collect(Collectors.toList());
        }
        return getDataTable(deviceInfos);
    }

    /**
     * 更新杀虫灯设备信息（用于控制设备模式）
     * @param scdDeviceDao
     * @return
     */
    @Override
    public Integer updateSCDDeviceInfo(SCDDeviceDao scdDeviceDao){
        if(scdDeviceDao.getModeHours().compareTo("001")>=0 && scdDeviceDao.getModeHours().compareTo("008")<=0){
            return scdDeviceMapper.updateSCDDeviceInfo(scdDeviceDao);
        }else{
            return null;
        }

    }

    /**
     * 更新设备状态
     */
    public void updateDeviceStatus(){
        List<SCDDeviceDao> scdDeviceDaos = scdDeviceMapper.selectAllDeviceInfo();
        for(SCDDeviceDao scdDeviceDao:scdDeviceDaos){
            String deviceId = scdDeviceDao.getDeviceId();
            Integer fanStatus = scdDeviceDao.getFanStatus();
            Integer ledStatus = scdDeviceDao.getLedStatus();
            if(devicePermiMapper.selectByDeviceSerial(deviceId) !=null){
                SCDDeviceDao selectSCDDeviceDao = scdDeviceMapper.selectSCDDeviceInfo(deviceId);
                SerDevicePermi devicePermiInfo = devicePermiMapper.selectByDeviceSerial(deviceId).setDeviceName(selectSCDDeviceDao.getNickName());
                long now = System.currentTimeMillis();
                Date updateTime = scdDeviceDao.getUpdateTime();
                if(now - updateTime.getTime() > 86400000){
                    devicePermiInfo.setOnlineState("离线");
                }else{
                    if(fanStatus ==1 || ledStatus == 1) {
                        devicePermiInfo.setOnlineState("在线");
                    }else {
                        devicePermiInfo.setOnlineState("休眠");
                    }
//                    devicePermiInfo.setOnlineState("在线");
                }
                devicePermiMapper.updateByPrimaryKeySelective(devicePermiInfo);
            }
            long now = System.currentTimeMillis();
            Date updateTime = scdDeviceDao.getUpdateTime();
            if(now - updateTime.getTime() > 86400000){
                scdDeviceDao.setDeviceStatus("离线");
                scdDeviceDao.setFanStatus(0);
                scdDeviceDao.setLedStatus(0);
            }else{
                if(fanStatus ==1 || ledStatus == 1) {
                    scdDeviceDao.setDeviceStatus("在线");
                }else {
                    scdDeviceDao.setDeviceStatus("休眠");
                }
//                scdDeviceDao.setDeviceStatus("在线");
            }
            scdDeviceMapper.updateSCDDeviceInfo(scdDeviceDao);
        }
    }


}
